package org.example.controller;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.BooleanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.common.configs.permission.Permission;
import org.example.common.configs.permission.PermissionIgnore;
import org.example.common.constants.WGConstant;
import org.example.common.session.SessionStore;
import org.example.common.session.UserContext;
import org.example.common.support.APIResponse;
import org.example.model.dto.FullUserDto;
import org.example.model.dto.ResetUserPwdDto;
import org.example.model.dto.UserLoginDto;
import org.example.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author weigang
 * @create 2022-11-08 15:37
 */

@RestController
@AllArgsConstructor
@RequestMapping("/sysUser")
@Permission(WGConstant.USER_MENU)
@Slf4j
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SessionStore sessionStore;

    /**
     * 用户登录
     *
     * @param loginParam login param
     * @return token
     */
    @PostMapping("/login")
    public APIResponse<String> login(@RequestBody @Valid UserLoginDto loginParam, HttpServletRequest request) {
        sysUserService.login(loginParam, request);
        Pair<String, FullUserDto> sessionPair = sessionStore.getUser(null, loginParam.getItCode());
        UserContext.putUser(sessionPair);
        log.info(" >>>>> end login , token : {} <<<<< " , UserContext.getToken());
        log.info(" >>>>> end login , user : {} <<<<< " ,UserContext.getUser());

//        logout(); // test postman重发请求调/logout的话，会是不同的Threadlocal取不到值
//        getCurrentUser(false);
//        getCurrentUser(true);

        return APIResponse.ok(sessionPair.getKey());
    }

    /**
     * 登出
     */
    @PutMapping("/logout")
    public APIResponse<String> logout() {
        log.info(" >>>>> logout , user : {} <<<<< " , UserContext.getUser());
        sessionStore.clearUser(UserContext.getToken());  // 退出登录，不清空UserContext。在有效时间内，再次打开网址直接进入
        return APIResponse.ok();
    }


    /**
     * 获取当前登录用户
     */
    @GetMapping("/current-user")
    @PermissionIgnore
    public APIResponse<FullUserDto> getCurrentUser(@RequestParam(defaultValue = "false") Boolean refresh) {
        String token = UserContext.getToken();
        log.info("current user token : [{}]" , token);
        if (BooleanUtil.isTrue(refresh)) {
            // 经过拦截器到这里，证明token一定是合法的
            log.info("refresh user token : [{}]" , UserContext.getToken());
            sessionStore.refresh(UserContext.getToken());
            Pair<String, FullUserDto> user = sessionStore.getUser(token, null);
            UserContext.putUser(user);
        }
        log.info("current user : [{}]" , UserContext.getUser());
        return APIResponse.ok(UserContext.getUser());
    }

    /**
     * 修改密码
     */
    @PutMapping("/reset-pwd")
    @PermissionIgnore
    public APIResponse<String> resetUserPwd(@RequestBody @Valid ResetUserPwdDto resetUserPwdParam) {
        sysUserService.resetUserPwd(resetUserPwdParam, UserContext.getUser());
        sessionStore.clearUser(UserContext.getToken());
        UserContext.clear(); // 重置密码清空UserContext
        return APIResponse.ok();
    }

}
